package com.lpy.mybatis.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lpy.mybatis.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

//访问数据库
@Mapper
public interface UserInfoMapper {

    // 返回数据列表
    @Select("select * from userinfo")
    List<UserInfo> queryUserList();

    // 当只有一个参数的时候，可以不和下面传的一样，其他必须一样
    @Select("select * from userinfo where id = #{id}")
    UserInfo queryUserInfo(Integer userId);

    @Select("select * from userinfo where id = ${id}")
    UserInfo queryUserInfo2(Integer userId);

    @Select("select * from userinfo where username = #{name}")
    UserInfo queryUserInfoByName1(String name);

    @Select("select * from userinfo where username = '${name}'")
    UserInfo queryUserInfoByName2(String name);

    // 重命名参数
    @Select("select * from userinfo where id = #{id} and delete_flag = #{deleteFlag}")
    UserInfo queryUserRename(@Param("id") Integer userId, @Param("deleteFlag") Integer deleteFlag);

    // 插入
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("INSERT INTO userinfo(username, `password`, age, gender, phone)" +
            "VALUES(#{userInfo.username}, #{userInfo.password}, #{userInfo.age}, #{userInfo.gender}, #{userInfo.phone}) ")
    Integer insert(@Param("userInfo") UserInfo userInfo);

    // 删除
    @Delete("DELETE FROM userinfo WHERE id = #{id}")
    void delete(@Param("id") Integer id);

    // 改
    @Update("UPDATE userinfo SET password = #{password} WHERE id = #{id}")
    void update(@Param("password") String password, @Param("id") Integer id);

}
